﻿using System;
using System.ComponentModel;
using System.Web.UI;

namespace CustomControlPractice.Controls.SearchBox
{
    public partial class SearchBox : UserControl
    {
        private static readonly object EventBeginSearch = new object();
        protected string SearchClickReference = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            SearchClickReference = Page.ClientScript.GetPostBackEventReference(searchButton, null);
        }

        #region public properties/methods/events
        /// <summary>
        /// Begin search, this occured after user click the search button.
        /// </summary>
        [Category("Action")]
        public event EventHandler<EventArgs> BeginSearch
        {
            add { Events.AddHandler(EventBeginSearch, value); }
            remove { Events.RemoveHandler(EventBeginSearch, value); }
        }
        #endregion

        protected void OnSearchButtonClick(object sender,EventArgs e)
        {
            RaiseEvent();
        }

        private void RaiseEvent()
        {
            var eventHandler = (EventHandler<EventArgs>)Events[EventBeginSearch];
            if (eventHandler != null) eventHandler(this, EventArgs.Empty);
        }
    }
}